//
//  LoginView.m
//  FKGW
//
//  Created by song ce on 2017/3/4.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "LoginView.h"
#import "UIImageView+WebCache.h"
#import "OSSHelper.h"
#import "AppDelegate.h"

@implementation LoginView
- (void)awakeFromNib {
    [super awakeFromNib];
    self.backgroundColor = [UIColor clearColor];
    self.userImageView.layer.cornerRadius = 67/2.0;
    self.userImageView.layer.masksToBounds = YES;
    [self changePhoto:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changePhoto:) name:@"changePhoto" object:nil];
}
-(void)changePhoto:(NSNotification*)noti{
    NSString *userPhoto;
    if(noti.object){
        NSDictionary *dic = noti.object;
        //用户名
        NSString *userName =dic[@"username"];
        if([userName isKindOfClass:[NSNull class]]){
            userName = @"这个人很忙，还没想好昵称";
        }
        self.nickNameLabel.text = userName;
        
        //用户手机号
        [AppDelegate appDealegate].userPhone = [NSString stringWithFormat:@"%@",dic[@"userPhone"]];
        
        //签名
        NSString *signature =dic[@"signature"];
        if([signature isKindOfClass:[NSNull class]]){
            signature = @"这个人很忙，没有时间写签名～";
        }
        self.signLabel.text = signature;
        
        //加载头像
        userPhoto = dic[@"userPhoto"];
        if([userPhoto isKindOfClass:[NSNull class]]){
            userPhoto = @"";
        }
          [self loadAvterImage:userPhoto];
    }else{
          [self loadAvterImage:userPhoto];
    }
}

-(void)loadAvterImage:(NSString*)userPhoto{
    self.userImageView.image = [UIImage imageNamed:@"icon_toxiang_yidenglu"];
    NSString *image_Name=  [[NSUserDefaults standardUserDefaults]objectForKey:USERID];
    if(image_Name.length){
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                             NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString* path1 = [documentsDirectory stringByAppendingPathComponent:image_Name];
        self.userImageView.image = [UIImage imageWithContentsOfFile:path1];
    }else{
        NSString *objectKey = userPhoto;
        if(!objectKey.length) return;
//        objectKey = [NSString stringWithFormat:@"user/header/%@%@",USERID,@".jpg"];
        __weak typeof(self) weakSelf = self;
        [[OSSHelper sharedHelper] OSSDownLoadWithObjectKey:objectKey downLoadBlock:^(NSData *data) {
            dispatch_async(dispatch_get_main_queue(), ^{
                UIImage *iamge =[UIImage imageWithData:data];
                weakSelf.userImageView.image =iamge;
                [weakSelf saveTolocal:iamge];
            });
        }];
    }
}


-(void)saveTolocal:(UIImage*)image{
    NSString *imageName;
    NSData *imageData = UIImageJPEGRepresentation(image, 1);
    imageName = [NSString stringWithFormat:@"%@.jpg",USERID];
    if(imageData.length>1024*1024){
        imageData = UIImageJPEGRepresentation(image, .1);
        DLog(@"--2--%zd",imageData.length/1024);
    }
    //本地头像路径
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent:imageName];
    BOOL isSuccess = [imageData writeToFile:path atomically:YES];
    if(isSuccess){
        //保存本地头像的路径
        [[NSUserDefaults standardUserDefaults]setObject:imageName forKey:USERID];
        [[NSUserDefaults standardUserDefaults]synchronize];
        DLog(@"本地保存路径---%@",path);
    }
}


-(void)dealloc{
    [[NSNotificationCenter defaultCenter ]removeObserver:self];
}




@end
